package org.onion.pattern.kit.springtool.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Profiles;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;

public class NotProfileCondition implements Condition {

    private boolean matches_(ConditionContext context, AnnotatedTypeMetadata metadata) {
        MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(MissingProfile.class.getName());
        if (attrs != null) {
            for (Object value : attrs.get("value")) {
                context.getEnvironment().getActiveProfiles();
                if (context.getEnvironment().getActiveProfiles().length<=0) {
                    return true;
                }
                if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) {
                    return true;
                }
            }
            return false;
        }

        return true;
    }

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return !matches_(context, metadata);
    }

}
